package com.ruoyi.client.aspect;

import cn.dev33.satoken.stp.StpUtil;
import com.ruoyi.client.annotation.Client_oper;
import com.ruoyi.client.annotation.TargetJson;
import com.ruoyi.client.domain.entity.BlogOper;
import com.ruoyi.common.core.utils.ServletUtils;
import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.json.utils.JsonUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Map;

@Aspect
//@AutoConfiguration
@Configuration
public class ClientOperAspect {
    /**
     * 处理完请求后执行
     *
     * @param joinPoint 切点
     */
    @AfterReturning(pointcut = "@annotation(controllerLog)", returning = "jsonResult")
    public void doAfterReturning(JoinPoint joinPoint, Client_oper controllerLog, Object jsonResult) {
        handleLog(joinPoint, controllerLog, null, jsonResult);
    }

    private void handleLog(JoinPoint joinPoint, Client_oper controllerLog, Object o, Object jsonResult) {
        BlogOper oper = new BlogOper();
        // 处理注解中的参数
        handleParam(controllerLog,oper);
        // 根据业务类型设置值
        handleBusiness(joinPoint,controllerLog,oper);
    }

    private void handleBusiness(JoinPoint joinPoint, Client_oper controllerLog, BlogOper oper) {
        int business = controllerLog.clientBusinessType().ordinal();
        Map<String, String> paramsMap = ServletUtils.getParamMap(ServletUtils.getRequest());
        // 获取方法签名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        // 获取方法参数
        Object[] args = joinPoint.getArgs();
        Parameter[] parameters = method.getParameters();
        for (int i = 0; i < parameters.length; i++) {
            Parameter parameter = parameters[i];
            TargetJson targetJson = parameter.getAnnotation(TargetJson.class);
            if (targetJson != null) {
                Object arg = args[i]; // 获取对应的参数值
                String s = JsonUtils.toJsonString(arg);
                oper.setTargetClass(arg.getClass().getName());
                oper.setTargetJson(s);
            }
        }
        SpringUtils.context().publishEvent(oper);
        handleByBusiness(business,paramsMap,oper);
    }

    private void handleByBusiness(int business, Map<String, String> paramsMap, BlogOper oper) {

    }

    private void handleParam(Client_oper controllerLog, BlogOper oper) {
        String title = controllerLog.title();
        Long userId = Long.valueOf((String) StpUtil.getLoginId());
        oper.setUserId(userId);
        oper.setTitle(title);
        oper.setSubTitle(LocalDateTime.now());
        oper.setBusinessNum(controllerLog.clientBusinessType().ordinal());
        oper.setBusinessType(controllerLog.clientBusinessType().getType());
    }
}
